package kr.ac.skhu.m.dao;

import java.sql.SQLException;
import java.util.List;

import org.springframework.jdbc.core.JdbcTemplate;

import kr.ac.skhu.m.domain.Comment;
import kr.ac.skhu.m.domain.Used;
import kr.ac.skhu.m.mapper.CommentM;
import kr.ac.skhu.m.mapper.UsedM;

public class UsedD extends JdbcTemplate {
	
	public List<Used> getUsedList(int pageNum) throws SQLException {
		
		int pageSize = 10;
		
		String sql = "SELECT " +
				"a.article_no, " +
				"a.title, " +
				"a.writer, " +
				"a.reg_date, " +
				"a.board_no, " +
				"a.password, " +
				"u.type, " +
				"u.goods_name, " +
				"u.price, " +
				"u.comment " +
				"FROM article a, used u " +
				"WHERE a.article_no = u.article_no " +
				"ORDER BY a.reg_date DESC " +
				"Limit " + pageNum + "," + pageSize;
		
		List<Used> list = (List<Used>)query(sql, new UsedM());
		
		return list;
		
	}
	
	public int getTotalPage() throws SQLException {
		String sql = "SELECT " +
				"count(*) " +
				"FROM article a, used u " +
				"WHERE a.article_no = u.article_no";
		
		int totalCount = queryForInt(sql);
		int totalPage = (totalCount/10);
		
		if(totalCount%10 > 0){
			totalPage = totalPage + 1;
		}
		
		return totalPage;
	}
	
	public Used getUsed(String aNo) throws SQLException {
		
		String sql = "SELECT " +
				"a.article_no, " +
				"a.title, " +
				"a.writer, " +
				"a.reg_date, " +
				"a.board_no, " +
				"a.password, " +
				"u.type, " +
				"u.goods_name, " +
				"u.price, " +
				"u.comment " +
				"FROM article a, used u " +
				"WHERE a.article_no = u.article_no " +
				"AND a.article_no = '" + aNo + "'"; 
		
		Used used = (Used)queryForObject(sql, new UsedM());
	
		return used;
	}
	
	public List<Comment> getCommentList(String aNo) throws SQLException {
		
		String sql = "SELECT " +
				"article_no, " +
				"writer, " +
				"reg_date, " +
				"comment, " +
				"password, " +
				"comment_no " +
				"FROM comment " +
				"ORDER BY reg_date DESC";
		
		List<Comment> list = (List<Comment>)query(sql, new CommentM());
		
		return list;
		
	}
}
